﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>指令</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    @Html.Style("~/scripts/layuiadmin/layui/css/layui.css")
    @Html.Style("~/scripts/layuiadmin/style/admin.css")
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="query-filter">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button class="layui-btn" id="btnAdd">新增</button>
                    </div>
                </div>
            </div>
            <div class="layui-card-body">
                <table id="LAY-app-download-list" lay-filter="LAY-app-download-list"></table>
            </div>
        </div>
    </div>
    <script type="text/html" id="download-edit">
        <div class="layui-form" lay-filter="download-edit" style="margin:10px;">
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="hidden" name="action" value="add">
                    <input type="hidden" name="id" value="0">
                    <input type="text" name="name" id="name" class="layui-input" lay-verify="required" placeholder="请输入名称" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">标识</label>
                <div class="layui-input-block">
                    <input type="text" name="value" class="layui-input" lay-verify="required" placeholder="请输入标识（数字，不可与其他指令标识重复）" />
                </div>
            </div>
        </div>
    </script>

    @Html.Script("~/scripts/layuiadmin/layui/layui.js")
    @Html.Script("~/scripts/clipboard.min.js")
<script>
        layui.config({
            base: '../../scripts/layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table','upload'], function () {
            var table = layui.table,
                upload = layui.upload,
                form = layui.form,
                $ = layui.$;

            initData();
            $('#btnAdd').click(function () { showEditDialog(undefined) });

            table.on('tool(LAY-app-download-list)', function (obj) {
                var layEvent = obj.event,
                    data = obj.data;

                if (layEvent === 'delete') {
                    layer.confirm('確定要刪除？', {
                        title: '提示',
                        btn: ['确定', '取消']
                    }, function (index) {
                        $.post('/admin/command/delete', { id: data.id }, function (result) {
                            if (result.code == 1) {
                                layer.close(index);
                                table.reload('downloadListTable');
                            } else {
                                layer.msg(result.msg);
                            }
                        });
                    });
                } else if (layEvent === 'update') {
                    data.action = "edit";
                    showEditDialog(data);
                }
            });

            function initData() {
                table.render({
                    elem: '#LAY-app-download-list',
                    url: '/admin/command/list',
                    cellMinWidth: 95,
                    method: 'post',
                    page: true,
                    limit: 20,
                    limits: [20, 50, 100],
                    id: "downloadListTable",
/*                    where: form.val("query-filter"),*/
                    response: {
                        statusName: 'code'
                        , statusCode: 1
                        , msgName: 'msg'
                        , countName: 'count'
                        , dataName: 'data'
                    },
                    cols: [[
                        { field: 'name', title: '名稱' },
                        { field: 'value', title: '标识' },
                        { field: 'addTime', title: '添加时间' },
                        {
                            title: '操作', width: 150, align: "center", templet: function (d) {
                                return '<div><button class="layui-btn layui-btn-xs" lay-event="update">编辑</button><button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">刪除</button><div>';
                            }
                        }
                    ]],
                    done: function () {
                        var btns = document.querySelectorAll('.key-clipboard');
                        var clipboard = new ClipboardJS(btns);

                        clipboard.on('success', function (e) {
                            layer.msg('已复制');
                        });
                    }
                });
            }

            function showEditDialog(download) {
                layer.open({
                    type: 1
                    , title: download ? '编辑' : '添加'
                    , content: $('#download-edit').html()
                    , area: ['600px', '240px']
                    , btn: ['确定', '取消']
                    , success: function () {
                        if (download) {
                            form.val("download-edit", download);
                        }

                        form.render();
                    }
                    , yes: function (index, layero) {
                        var formData = form.val("download-edit");

                        $.post('/admin/command/edit', formData, function (data) {
                            if (data.code == 1) {
                                layer.close(index);
                                table.reload('downloadListTable');
                            } else {
                                layer.msg(data.msg);
                            }
                        });
                    }
                });
            }
        });
</script>
</body>
</html>